package org.jeecg.modules.online.cgform.c;

import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.config.JeecgBaseConfig;
import org.jeecg.modules.online.auth.service.IOnlAuthPageService;
import org.jeecg.modules.online.cgform.service.IOnlCgformFieldService;
import org.jeecg.modules.online.cgform.service.IOnlCgformHeadService;
import org.jeecg.modules.online.cgform.service.IOnlCgformSqlService;
import org.jeecg.modules.online.cgform.service.IOnlineJoinQueryService;
import org.jeecgframework.codegenerate.database.DbReadTableUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Primary;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController("onlCgformApiController")
@RequestMapping({"/online/cgform/api"})
@Primary
public class a {
    private static final Logger a = LoggerFactory.getLogger(a.class);
    @Autowired
    private IOnlCgformHeadService onlCgformHeadService;
    @Autowired
    IOnlineJoinQueryService onlineJoinQueryService;
    @Autowired
    private IOnlCgformFieldService onlCgformFieldService;
    @Autowired
    private IOnlCgformSqlService onlCgformSqlService;
    @Autowired
    private IOnlAuthPageService onlAuthPageService;
//    @Lazy
//    @Autowired
//    private ISysBaseAPI sysBaseAPI;
//    @Autowired
//    private IOnlineService onlineService;
    @Value("${jeecg.path.upload}")
    private String upLoadPath;
    @Value("${jeecg.uploadType}")
    private String uploadType;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private JeecgBaseConfig jeecgBaseConfig;
//    @Autowired
//    private IOnlCgformAiService iOnlCgformAiService;
    //@RequiresPermissions({"online:form:generateCode"})
    @PostMapping({"/codeGenerate"})
    public Result<?> b(@RequestBody JSONObject var1) {
        System.out.println("已经进入该方法了,自定义的方法"+var1.toJSONString());
        org.jeecg.modules.online.cgform.model.d var2 = (org.jeecg.modules.online.cgform.model.d)JSONObject.parseObject(var1.toJSONString(), org.jeecg.modules.online.cgform.model.d.class);
        boolean var3 = this.jeecgBaseConfig.getFirewall() != null ? this.jeecgBaseConfig.getFirewall().getDataSourceSafe() : false;
        if (var3 && !DbReadTableUtil.getProjectPath().equals(var2.getProjectPath())) {
            var2.setProjectPath(DbReadTableUtil.getProjectPath());
            a.warn("数据源安全模式下，自定义代码生成路径无效，使用全局配置的路径 ::{}", DbReadTableUtil.getProjectPath());
        }

        List var4 = null;

        try {
            if ("1".equals(var2.getJformType())) {
                var4 = this.onlCgformHeadService.generateCode(var2);
            } else {
                var4 = this.onlCgformHeadService.generateOneToMany(var2);
            }

            LoginUser var5 = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            String var6 = var5.getUsername() + var2.getTableName() + oConvertUtils.randomGen(16);
            Result var7 = Result.ok(var4);
            String var8 = var2.getProjectPath().replaceAll("\\\\", "/");
            this.redisUtil.set(var6, var8, 1800L);
            var7.setMessage(var6);
            return var7;
        } catch (Exception var9) {
            var9.printStackTrace();
            return Result.error(var9.getMessage());
        }
    }
}
